package com.friend.chat.page.view

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.fragment.app.Fragment
import com.friend.chat.R
import com.friend.chat.base.BaseActivity
import com.friend.chat.chat.CHAT_SYSTEM
import com.friend.chat.databinding.ActivityMainBinding
import com.friend.chat.media.audio.AudioActivity
import com.friend.chat.media.audio.CALL_AUDIO_ACK
import com.friend.chat.media.audio.PICKUP_AUDIO_ACK
import com.friend.chat.media.video.RING_VIDEO_ACK
import com.friend.chat.media.video.SingleVideoActivity
import com.friend.chat.page.view.main.*
import com.friend.chat.page.vm.MainViewModel
import com.friend.chat.service.WebsocketService
import com.friend.chat.websocket.ChatMessageBean
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author sam
 * @des 首页
 */
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
    private var mServiceIntent: Intent? = null
    private val mFragments = ArrayList<Fragment>()

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MainActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun yourViewModel(): MainViewModel = MainViewModel()

    override fun yourViewBing(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun init() {
        initFragment()

        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this)
        }

        mServiceIntent = Intent(this, WebsocketService::class.java)
        startService(mServiceIntent)

       /* //给sam打电话
        mViewBinding.btAudioCallSam.setOnClickListener {
            AudioActivity.open(this,"12345678",true)
        }

        //给bob打电话
        mViewBinding.btAudioCallBob.setOnClickListener {
            AudioActivity.open(this,"123433",true)
        }

        //聊天
        mViewBinding.btChatSam.setOnClickListener {
            val userBean = SPUtils.getInstance().getObject("user") as UserBean
            Log.e("--->uid",userBean.uid)
            val intent = Intent(this,SingleChatActivity::class.java)
            intent.putExtra("userId",userBean.uid)
            intent.putExtra("toUid","12345678")
            startActivity(intent)
        }*/
    }

    private fun initFragment() {
        mFragments.let {
            it.add(ChatRoomListFragment())
            it.add(ContactsFragment())
            it.add(DiscoverFragment())
            it.add(MeFragment())
        }
        mViewBinding.vpMain.let {
            it.adapter = MyPagerFragmentAdapter(supportFragmentManager, mFragments)
            it.currentItem = 0
            it.offscreenPageLimit = 4
        }

        mViewBinding.rgMain.setOnCheckedChangeListener { _, id ->
            when (id) {
                R.id.rb_main_1 -> {
                    mViewBinding.vpMain.setCurrentItem(0, false)
                }
                R.id.rb_main_2 -> {
                    mViewBinding.vpMain.setCurrentItem(1, false)
                }
                R.id.rb_main_3 -> {
                    mViewBinding.vpMain.setCurrentItem(2, false)
                }
                R.id.rb_main_4 -> {
                    mViewBinding.vpMain.setCurrentItem(3, false)
                }
                else -> {
                    Log.e("--->", "出错了")
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: ChatMessageBean?) {
       event?.apply {
           if (event.type ==0&& CALL_AUDIO_ACK == event.message){
               //电话来了了
               AudioActivity.open(this@MainActivity,event.userId,false)
           }

            if (event.type == CHAT_SYSTEM && RING_VIDEO_ACK == event.message){
                //视频电话来了
                SingleVideoActivity.open(this@MainActivity,event.userId,false)
            }

           if (event.type ==0&& PICKUP_AUDIO_ACK == event.message){
               //对方已接听，开始录制

           }
       }
    }


}